#include "user.h"

user::user()
{
}

user::~user()
{
    for (int i = 0; i < m_gameList.size(); i++)
        if (m_gameList[i])
            delete m_gameList[i];
}

void user::setGameList(QVector<QString > gameL)
{
    for (int i = 0; i < gameL.size(); i++)
    {
        try
        {
            m_gameList.push_back(new Games(gameL[i]));
        }
        catch (int err)
        {
            if (err == 4242)
            {
                delete m_gameList[m_gameList.size() - 1];
                m_gameList.pop_back();
            }
        }
    }
}

void user::setParam(QString pseudo, QString pseudoV, QString email, bool dev, int id)
{
    m_pseudo = pseudo;
    m_visiblePseudo = pseudoV;
    m_email = email;
    m_id = id;
    m_dev = dev;
}

QString user::getPseudo() {return m_pseudo;}

QString user::getVisiblePseudo() {return m_visiblePseudo;}

QString user::getEmail() {return (m_email);}

bool    user::isDev() {return m_dev;}

int     user::getId() {return m_id;}

QVector<Games *> &user::getGameList() {return m_gameList;}

void user::downloadImage(QString httpimage)
{
    QNetworkAccessManager   *qnam = new QNetworkAccessManager();
    QNetworkRequest         qnr;
    qnr.setUrl(QUrl(httpimage));
    qnam->get(qnr);
    QObject::connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(saveImage(QNetworkReply*)));
}

void user::saveImage(QNetworkReply *netRe)
{
    QFile   stream("Image/user.png");

    if (!stream.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        QMessageBox::warning(new QWidget, "Erreur de sauvegarde.", "UseLess Game n'a pas réussi à sauvegarder l'image.");
    }
    else
    {
        QTextStream text(&stream);
        text << netRe;
        stream.close();
    }
}
